package com.ky.facescan.utils;

import android.app.Activity;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 软键盘相关辅助类
 */

public class KeyBoardUtils {

    /**
     * 打开软键盘
     *
     * @param view    控件，一般是输入框
     * @param context 上下文
     */
    public static void openKeyBoard(View view, Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 关闭软键盘
     *
     * @param view    控件
     * @param context 上下文
     */
    public static void closeKeyBoard(View view, Context context) {
//        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            assert view != null;
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            /** here */
            view.clearFocus();
        }
    }

    public static void hide(Activity activity) {
        if (activity.getCurrentFocus() == null) {
            return;
        }
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }

    /**
     * 输入法是否显示
     *
     * @param editText
     * @return
     */
    public static boolean showOrHide(EditText editText) {
        boolean bool = false;
        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            bool = true;
        }
        return bool;
    }

    /**
     * 控制点击EditText区域外隐藏键盘。此方法要和activity的dispatchTouchEvent方法配合使用
     *
     * @param view
     * @param event
     * @return
     */

    public static boolean isShouldHideInput(View view, MotionEvent event) {
        if (view != null && (view instanceof EditText)) {
            int[] l = {0, 0};
            view.getLocationInWindow(l);
            int left = l[0], top = l[1],
                    bottom = top + view.getHeight(), right = left + view.getWidth();
            if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
                //点击EditText的事件，忽略它
                ((EditText) view).setCursorVisible(true);
                return false;
            } else {
                return true;
            }
        }
        //如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    public static boolean isActive(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        return imm.isActive();
    }

    /**
     * 通过定时器强制显示或隐藏系统键盘
     *
     * @param editText
     * @param open     强制显示为TRUE，强制隐藏为False;
     * @param time
     */
    public static void timerKeyBoard(final EditText editText, final boolean open, int time) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (open) {
                    imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
                } else {
                    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                }
            }
        }, time);
    }

    /**
     * 通过定时器强制隐藏虚拟键盘
     *
     * @param view
     * @param time
     */
    public static void timerHideKeyBoard(final View view, int time) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive()) {
                    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
                }
            }
        }, time);
    }
}
